/**
 * 
 */
package com.hcmu.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.google.code.microlog4android.Logger;
import com.hcmu.R;
import com.hcmu.dto.DatabaseHelper;
import com.hcmu.dto.StoryDTO;
import com.hcmu.dto.CategoryDTO;

import android.content.Context;
import android.database.Cursor;
import android.database.Observable;

/**
 * @author SEVEN
 *
 */
public class MainHomeModel extends Observable {
	private Context _context;
	private DatabaseHelper _dbHelper;
	
	
	public MainHomeModel(Context context) {
		this._context = context;
		this._dbHelper = new DatabaseHelper(context);
	}
	
	/**
	 * Lay ve danh sach cac loai truyen ( categories )
	 * @return
	 */
	public List<CategoryDTO> getStoryType(){
		List<CategoryDTO> list = new ArrayList<CategoryDTO>();
		_dbHelper.openToRead();
		Cursor cursor = _dbHelper.getAllCategory();
		cursor.moveToFirst();
		for (int i = 0; i < cursor.getCount(); i++) {
			HashMap<String, String> item = new HashMap<String, String>();
			CategoryDTO dto = new CategoryDTO();
			dto.setId(Integer.parseInt(cursor.getString(0)));
			dto.setTitle(cursor.getString(1));
			list.add(dto);
			cursor.moveToNext();
		}
		_dbHelper.close();
		
		return list;
	}
	
	/**
	 * Get stories list by id of the type
	 * @param type_id : id of the type
	 * @return the list containing stories 
	 */
	public ArrayList<StoryDTO> getStoryList(String type_id) {
		ArrayList<StoryDTO> list = new ArrayList<StoryDTO>();
		_dbHelper.openToRead();
		Cursor cursor = _dbHelper.getStories(type_id);
		cursor.moveToFirst();
		for (int i = 0; i < cursor.getCount(); i++) {
			StoryDTO dto = new StoryDTO();
			dto.setIcon(R.drawable.ic_funny);
			dto.setId(Integer.parseInt(cursor.getString(0)));
			dto.setTitle(cursor.getString(1));
			list.add(dto);
			cursor.moveToNext();
		}
		_dbHelper.close();
		return list;
	}
	
	
	public Boolean deleteStory(int sid){
		_dbHelper.openToWrite();
		int result = _dbHelper.deleteStory(sid);
		// result = 1 if delete success. else result = 0;
		_dbHelper.close();
		return (result==1) ? true : false;
	}

}
